package com.factory.test.m5622;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;


import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityM5622ElectricBalanceBinding;
import com.factory.test.util.FileUtils;

import java.util.Timer;
import java.util.TimerTask;

public class ElectricBalanceActivity extends BaseTestActivity {

    private ActivityM5622ElectricBalanceBinding binding;

    private static final String VIN1_PATH = "/sys/devices/platform/weight2/iio:device1/in_voltage0_raw";
    private static final String VIN2_PATH = "/sys/devices/platform/weight1/iio:device0/in_voltage0_raw";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_m5622_electric_balance);

        myTask = new MyTask();
        Timer timer = new Timer();
        timer.schedule(myTask, 1000, 200);
    }

    private MyTask myTask;
    private class MyTask extends TimerTask {
        @Override
        public void run() {
            String v1 = FileUtils.readFile2String(VIN1_PATH);
            String v2 = FileUtils.readFile2String(VIN2_PATH);
            handler.post(() -> {
                binding.value1.setText("weight1: " + v1);
                binding.value2.setText("weight2: " + v2);
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(myTask != null){
            myTask.cancel();
        }
    }
}
